<template>
<div class="main_box" style="padding:10px">
  <div class="add_wrap">
    <div class="tit">
      <h2>基础信息</h2>
      <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
    </div>
    <el-form :model="addForm" ref="addForm" label-width="200px" :rules="roleRules">
      <el-row>
        <el-form-item label="上级公司：" prop="parentDepartId">
            <el-select v-model="addForm.parentDepartId" :clearable="true" placeholder="请选择上级公司">
              <el-option v-for="item in listDepart" :key="item.departId" :label="item.departName" :value="item.departId"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="公司名称：" prop="departName">
          <el-input :clearable="true" v-model="addForm.departName" @keydown.native="keydown($event)"></el-input>
        </el-form-item>
        <el-form-item label="公司负责人：" prop="managerUserId">
          <el-select v-model="addForm.managerUserId" :clearable="true" placeholder="请选择公司负责人">
            <el-option v-for="item in listAdmin" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input type="textarea" :clearable="true" v-model="addForm.remark"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button plain type="primary" @click="sureClick">保 存</el-button>
          <el-button @click="returnList">取 消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
 </div>
</template>

<script>
import * as message from "@/constants/message";
import DepartApi from "@/api/system/depart";
import AuthApi from "@/api/system/auth";
export default{
  data(){
      return {
        addForm:{
          departId: "",
          parentDepartId: "",
          departName: "",
          managerUserId: "",
          managerUserName: "",
          remark:""
        },
        listDepart: [],
        listAdmin: [],
        roleRules: {
          parentDepartId: [
            { required: true, message: "请选择上级公司", trigger: "change" },
          ],
          departName: [
            { required: true, message: "请输入公司名称", trigger: "blur" }
          ],
          managerUserName: [
            { required: true, message: "请输入公司负责人", trigger: "blur" }
          ]
        }
      }
  },
  mounted(){
    this.init();
  },
  methods: {
    init(){
      this.getDepartData();
      this.getAdminData();
      if(this.$route.query.id){
        this.addForm.departId = this.$route.query.id
        this.getData();
      }
    },
    keydown(e) {
      if (e.keyCode == 32) {
        e.returnValue = false;
      }
    },
    getDepartData(){
      DepartApi.listDepart()
        .then(res => {
          this.listDepart = res.data
      })
    },
    getAdminData(){
      AuthApi.listAdminByRole()
        .then(res => {
          this.listAdmin = res.data
      })
    },
    getData(){
      DepartApi.getDepartInfo({departId:this.$route.query.id})
        .then(res => {
          console.table(res.data)
          this.$Utils.bindingBean(this.addForm, res.data);
          this.addForm.managerUserId = parseInt(res.data.managerUserId);
          this.addForm.parentDepartId = res.data.parentDepartId;
        })
    },
    sureClick() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
            .then(() => {
              if (this.addForm.departId){
                this.editData();
              }else{
                this.addData();
              }
            });
        }
      })
    },
    addData() {
      let data = {
        parentDepartId: this.addForm.parentDepartId,
        departName: this.addForm.departName,
        managerUserId: this.addForm.managerUserId,
        remark: this.addForm.remark
      };
      DepartApi.addDepart(data)
        .then(data => {
          this.$notify.success({
            title: message.TITLE_SUCCESS,
            message: message.MSG_ACTION_SUCCESS
          });
        this.returnList()
      })
    },
    editData() {
      let data = {
        departId:this.$route.query.id,
        parentDepartId: this.addForm.parentDepartId,
        departName: this.addForm.departName,
        managerUserId: this.addForm.managerUserId,
        remark: this.addForm.remark
      };
      console.log(data,144)
      DepartApi.editDepart(data)
        .then(res => {
          this.$Utils.notifySuccess()
          this.returnList()
        })
    },
    returnList(){
      this.$store.dispatch('delView', this.$route);
      this.$router.push({path: '/sys/departManagement'})
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/assets/css/views/add.scss";
</style>
